import Vue from 'vue'
import VueRouter from 'vue-router'
// 正常路由导入
import Login from '@/components/login'
// 路由懒加载导入
// const Login = () => import(/* webpackChunkName:"login_home_welcome" */ '@/components/login')
import Home from '@/components/home'
// const Home = () => import(/* webpackChunkName:"login_home_welcome" */ '@/components/home')
import Welcome from '@/components/welcome'
// const Welcome = () => import(/* webpackChunkName:"login_home_welcome" */ '@/components/welcome')

import Users from '@/components/user/users'
// const Users = () => import(/* webpackChunkName:"Users_Rights_Goods" */ '@/components/user/users')
import Rights from '@/components/rights/rights'
// const Rights = () => import(/* webpackChunkName:"Users_Rights_Goods" */ '@/components/rights/rights')
import Goods from '@/components/goods/goods'
// const Goods = () => import(/* webpackChunkName:"Users_Rights_Goods" */ '@/components/goods/goods')

import Orders from '@/components/orders/orders'
// const Orders = () => import(/* webpackChunkName:"Orders_Reports_Roles" */ '@/components/orders/orders')
import Reports from '@/components/reports/reports'
// const Reports = () => import(/* webpackChunkName:"Orders_Reports_Roles" */ '@/components/reports/reports')
import Roles from '@/components/roles/roles'
// const Roles = () => import(/* webpackChunkName:"Orders_Reports_Roles" */ '@/components/roles/roles')

import Categories from '@/components/categories/categories'
// const Categories = () => import(/* webpackChunkName:"Categories_Params_Add" */ '@/components/categories/categories')
import Params from '@/components/params/params'
// const Params = () => import(/* webpackChunkName:"Categories_Params_Add" */ '@/components/params/params')
import Add from '@/components/goods/add'
// const Add = () => import(/* webpackChunkName:"Categories_Params_Add" */ '@/components/goods/add')

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [{
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [{
      path: '/welcome',
      component: Welcome
    },
    {
      path: '/users',
      component: Users
    },
    {
      path: '/rights',
      component: Rights
    },
    {
      path: '/goods',
      component: Goods
    },
    {
      path: '/orders',
      component: Orders
    },
    {
      path: '/roles',
      component: Roles
    },
    {
      path: '/categories',
      component: Categories
    },
    {
      path: '/params',
      component: Params
    },
    {
      path: '/reports',
      component: Reports
    },
    {
      path: '/add',
      component: Add
    }
    ]
  }
  ]
})

// 设置路由守卫，防止直接访问需要登陆的路由地址
router.beforeEach((to, from, next) => {
  // to 访问路径 from 从哪个路由跳转过来的 next 放行函数 next()放行 next('/login')不放行，且强制跳转到其他页面
  if (to.path === '/login') return next() // 表示用户在任何页访问登录页，直接放行
  // 获取token值，没有的话就跳转到登录页
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router
